package com.fuyao.cloud.personnel.problem.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fuyao.cloud.common.core.util.R;
import com.fuyao.cloud.common.excel.annotation.RequestExcel;
import com.fuyao.cloud.common.excel.annotation.ResponseExcel;
import com.fuyao.cloud.common.log.annotation.SysLog;
import com.fuyao.cloud.personnel.api.constant.ProblemFeedbackConstant;
import com.fuyao.cloud.personnel.api.entity.ProblemFeedback;
import com.fuyao.cloud.personnel.api.vo.excel.ProFeedbackExcelVO;
import com.fuyao.cloud.personnel.api.vo.excel.ProblemFeedbackExportVO;
import com.fuyao.cloud.personnel.problem.service.ProblemFeedbackService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 整改反馈
 *
 * @author pigx code generator
 * @date 2022-04-26 10:13:54
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/feedback" )
@Api(value = "feedback", tags = "整改反馈管理")
public class ProblemFeedbackController {

    private final ProblemFeedbackService problemFeedbackService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param problemFeedback 整改反馈
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('problem_feedback_view')" )
    public R getProblemFeedbackPage(Page page, ProblemFeedback problemFeedback) {
        return R.ok(problemFeedbackService.page(page, Wrappers.query(problemFeedback)));
    }


    /**
     * 通过id查询整改反馈
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('problem_feedback_view')" )
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(problemFeedbackService.getById(id));
    }

    /**
     * 新增整改反馈
     * @param problemFeedback 整改反馈
     * @return R
     */
    @ApiOperation(value = "新增整改反馈", notes = "新增整改反馈")
    @SysLog("新增整改反馈" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('problem_feedback_add')" )
    public R save(@RequestBody ProblemFeedback problemFeedback) {
        return R.ok(problemFeedbackService.save(problemFeedback));
    }

    /**
     * 修改整改反馈
     * @param problemFeedback 整改反馈
     * @return R
     */
    @ApiOperation(value = "修改整改反馈", notes = "修改整改反馈")
    @SysLog("修改整改反馈" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('problem_feedback_edit')" )
    public R updateById(@RequestBody ProblemFeedback problemFeedback) {
        return R.ok(problemFeedbackService.updateById(problemFeedback));
    }

    /**
     * 通过id删除整改反馈
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除整改反馈", notes = "通过id删除整改反馈")
    @SysLog("通过id删除整改反馈" )
    @DeleteMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('problem_feedback_del')" )
    public R removeById(@PathVariable Long id) {
        return R.ok(problemFeedbackService.removeById(id));
    }


    /**
     * 导出excel 表格
     * @param problemFeedback 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @PostMapping("/export")
    @PreAuthorize("@pms.hasPermission('problem_feedback_export')" )
    public List<ProblemFeedbackExportVO> export(ProblemFeedback problemFeedback ) {
        List<ProblemFeedback> feedbackList = this.problemFeedbackService.list(Wrappers.query(problemFeedback));
        List<ProblemFeedbackExportVO> exportVOList = new ArrayList<>();
        if(CollUtil.isNotEmpty(feedbackList)){
            feedbackList.forEach(problemFeedback1 -> {
                ProblemFeedbackExportVO exportVO = new ProblemFeedbackExportVO();
                BeanUtil.copyProperties(problemFeedback1,exportVO);
                exportVOList.add(exportVO);
            });
        }
        return exportVOList;
    }


    @GetMapping("/submitStatus/{id}")
    @PreAuthorize("@pms.hasPermission('problem_rectify_edit')")
    public R submitStatus(@PathVariable("id") Long id) {
        boolean res = problemFeedbackService.updateStatus(id, ProblemFeedbackConstant.HAS_BEEN_SUBMITTED);
        if (res){
            return R.ok();
        }
        return R.failed("出现错误联系管理员");
    }

    /**
     * 导入
     * @return R
     */
    @PostMapping("/import")
    @PreAuthorize("@pms.hasPermission('problem_feedback_export')")
    public R importFeedback(@RequestExcel List<ProFeedbackExcelVO> excelVOList, BindingResult bindingResult) {
        return problemFeedbackService.importFeedback(excelVOList, bindingResult);
    }
}
